﻿/*
 * aspark
 * Email:mwyso@126.com
 * http://quicksl.codeplex.com/
 */

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.Threading;
using System.Collections.Generic;
using System.Linq;

namespace QuickSL
{
    public abstract class AsyncQueue<T> where T : AsyncItem
    {
        ManualResetEvent _enqueueEvent;

        private List<T> _queue = null;

        protected AsyncQueue()
        {
            _enqueueEvent = new ManualResetEvent(false);
            _queue = new List<T>();

            Thread dispatherThread = new Thread(new ThreadStart(Process));
            dispatherThread.IsBackground = true;
            dispatherThread.Start();
        }

        /// <summary>
        /// Clear all
        /// </summary>
        public virtual void Clear()
        {
            lock (_queue)
            {
                _queue.Clear();
            }
        }

        /// <summary>
        /// Enqueue a action
        /// </summary>
        /// <param name="operation"></param>
        public virtual void Enqueue(T item)
        {
            if (item == null)
            {
                //throw new Exception("Parameter can't be Null");
                return;
            }

            lock (_queue)
            {
                _queue.Add(item);
            }

            //set a signal, allow to process the delegate
            _enqueueEvent.Set();

            //Insert(item, _queue.Count - 1);
        }

        public virtual void Enqueue(T item, int index)
        {
            if (item == null)
            {
                //throw new Exception("Parameter can't be Null");
                return;
            }

            lock (_queue)
            {
                _queue.Insert(index, item);
            }

            //set a signal, allow to process the delegate
            _enqueueEvent.Set();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public virtual bool Remove(T item)
        {
            lock (_queue)
            {
                if (_queue.Contains(item))
                {
                    return _queue.Remove(item);
                }
            }

            return false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected T Peek()
        {
            lock (_queue)
            {
                return _queue.FirstOrDefault();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected T Dequeue()
        {
            lock (_queue)
            {
                T item = _queue.FirstOrDefault();
                _queue.Remove(item);
                return item;
            }
        }

        /// <summary>
        /// process the delegate queue
        /// </summary>
        private void Process()
        {
            while (true)
            {
                //waiting for delegate enqueue
                _enqueueEvent.WaitOne();

                OnProcessing();

                lock (_queue)
                {
                    //if _delegateQueue has not child, make the thread wait for enqueue
                    if (_queue.Count == 0)
                    {
                        _enqueueEvent.Reset();
                    }
                }

                //OnProcessed();
            }
        }

        protected abstract void OnProcessing();

        //protected abstract void OnProcessed();
    }
}
